package com.example.antivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.Model.music.Music;
import com.example.Model.music.MusicComment;
import com.example.UI.Myimageview;
import com.example.UI.Mylistview;
import com.example.adapter.MusicComAdapter;

import com.example.retrofitinterface.music.MusicComInter;
import com.example.retrofitinterface.music.MusicInter;
import com.example.utils.GlideCircleTransform;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import sdstcom.lbq.one.R;
import sdstcom.lbq.one.lbq.Databeans.DbBean;
import sdstcom.lbq.one.lbq.LBQapplication;

public class MusicActivity extends AppCompatActivity {

    //private String url="http://v3.wufazhuce.com:8000/api/music/detail/1173?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String url1 ="http://v3.wufazhuce.com:8000/api/music/detail/";
    private String url2;
    private String url3 = "?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String url;
    private String commenturl1="http://v3.wufazhuce.com:8000/api/comment/praiseandtime/music/";
    private String commenturl3="/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private String commenturl;
    //private String commenturl = "http://v3.wufazhuce.com:8000/api/comment/praiseandtime/music/1173/0?platform=android&version=4.0.2&uuid=ffffffff-acdc-5a02-0a04-a6b66eed847f&channel=91";
    private Toolbar tb;
    private ImageView coveriv;
    private Myimageview myiv;
    private TextView title;
    private TextView album;
    private TextView singer;
    private TextView storytitle;
    private TextView storyauhor;
   private com.tencent.smtt.sdk.WebView wv;
    private TextView editor;
    private TextView editoremail;
    private ImageView singeriv;
    private TextView singername;
    private TextView singerdesc;
    private Mylistview lv;
    private MusicComAdapter adapter;
    private ArrayList<MusicComment.DataBeanX.DataBean> list;



    private String DBtitle;
    private DbManager dbManager;
    private boolean ifpress = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_music);
        tb = (Toolbar) findViewById(R.id.music_tb);
        tb.setTitle("");
        tb.setNavigationIcon(R.mipmap.return_image_4_0);
        //返回监听
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        init();
    }

    private void init() {
        //coveriv = (ImageView) findViewById(R.id.music_cover_iv);
        url2=getIntent().getStringExtra("itemid");
        url = url1+url2+url3;
        commenturl =commenturl1+url2+commenturl3;

        myiv = (Myimageview) findViewById(R.id.myiv);
        title = (TextView) findViewById(R.id.music_title_tv);
        album = (TextView) findViewById(R.id.music_album_tv);
        singer = (TextView) findViewById(R.id.music_authorname_tv);
        storytitle = (TextView) findViewById(R.id.music_storytitle_tv);
        storyauhor = (TextView) findViewById(R.id.music_storyauthor_tv);
       wv = (com.tencent.smtt.sdk.WebView) findViewById(R.id.music_story_wb);
        initwv();
        editor = (TextView) findViewById(R.id.music_edit_tv);
        editoremail = (TextView) findViewById(R.id.music_edi_emial_tv);
        singeriv = (ImageView) findViewById(R.id.music_singer_iv);
        singername = (TextView) findViewById(R.id.music_singer);
        singerdesc = (TextView) findViewById(R.id.music_singer_desc);
        //评论适配器及list初始化
        list = new ArrayList<>();
        adapter = new MusicComAdapter(list,this);
        lv = (Mylistview) findViewById(R.id.music_comment_lv);
        lv.setAdapter(adapter);
        dbManager= x.getDb(((LBQapplication)getApplication()).config);
        loadmusic(url);
        loadcomment();
    }
    //评论下载
    public void loadcomment() {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MusicComInter.class).getcomment(commenturl).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<MusicComment>() {
                    @Override
                    public void call(MusicComment musicComment) {
                        list.addAll(musicComment.getData().getData());
                        adapter.notifyDataSetChanged();
                    }
                });

    }
    private void initwv() {
//        Log.e("PATH",getIntent().getStringExtra("path"));

        //wv.loadUrl(getIntent().getStringExtra("path"));
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptEnabled(true);

        // 设置加载进来的页面自适应手机屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(true);

        settings.setTextSize(WebSettings.TextSize.LARGEST);

        wv.setWebViewClient(new WebViewClient(){

            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }
        });
    }

    //音乐的下载
    private void loadmusic(String url) {
        new Retrofit.Builder().baseUrl("http://v3.wufazhuce.com:8000/").
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build().create(MusicInter.class).getmusic(url).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Music>() {
            @Override
            public void call(Music music) {
                //设置歌手等相关信息

                title.setText(music.getData().getTitle());
                album.setText(music.getData().getAlbum());
                singer.setText(music.getData().getAuthor().getUser_name());
                //设置文章相关

                DBtitle=music.getData().getStory_title();
                storytitle.setText(music.getData().getStory_title());
                storyauhor.setText(music.getData().getStory_author().getUser_name());
                wv.loadData(music.getData().getStory(),"text/html; charset=UTF-8",null);
                editor.setText(music.getData().getCharge_edt());
                editoremail.setText(music.getData().getEditor_email());
                //设置歌手相关信息
                //下载并设置歌手头像
                //图片圆角工厂
                GlideCircleTransform transform = new GlideCircleTransform(getApplication());
                if (music.getData().getStory_author().getWeb_url() != null) {
                    Glide.with(getApplication()).load(music.getData().getStory_author().getWeb_url() ).transform(transform).into(singeriv);
                }
                singername.setText(music.getData().getStory_author().getUser_name());
                singerdesc.setText(music.getData().getStory_author().getSummary());

                //下载专辑封面
                try {

                    Glide.with(getApplication()).load(music.getData().getCover()).asBitmap().into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            int height =  resource.getHeight();
                            int width = resource.getWidth();
                            Bitmap bitmap = Bitmap.createBitmap(resource,height/2,width/2,400,400);
                            Log.e("xxx", "图片一半高度: "+height/2 );
                           myiv.getbitmap(bitmap);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void collection(View view) throws DbException {
        if (ifpress == false) {
            view.setPressed(true);
            DbBean dbBean = new DbBean();
            dbBean.setItemid(url2);
            dbBean.setType("music");
            dbBean.setTitle(DBtitle);
            dbBean.setSubtype("music");
            DbBean bean=dbManager.findById(DbBean.class,url2);
//            Log.e("TS", "collection: "+bean);
//            String sql="SELECT * FROM myonetable WHERE itemid="+url2;
            if (bean==null)
            {
                dbManager.save(dbBean);
                Toast.makeText(this,"收藏完成",Toast.LENGTH_SHORT).show();}
            ifpress=true;
        }else if (ifpress==true)
        {
            view.setPressed(false);
            dbManager.deleteById(DbBean.class,url2);
            ifpress=false;
            Toast.makeText(this,"取消收藏",Toast.LENGTH_SHORT).show();
        }
    }
}
